﻿#region License statement
// NJamb, a specification and delta-specification DSL
// Copyright (c) 2010-2011, Mark Knell
// Published under the MIT License; all other rights reserved
#endregion

#region using...
using System;
using System.Linq.Expressions;
using NJamb.Parser.SubjectBuilders;
using NJamb.Specifications.Void;
using Stile.Patterns.SelfDescribingPredicates;
#endregion

namespace NJamb.Parser.Subjects.Void
{
    public class VoidSubject<THost> : Subject<THost, IVoidCollaboratingSubject<THost>, IVoidSpecification<THost>>,
        IVoidSubject<THost>,
        IVoidCollaboratingSubject<THost>
        where THost : class
    {
        public VoidSubject(ISubjectBuilder<THost> subjectBuilder, Expression<Action<THost>> action)
            : base(subjectBuilder.Xray.Describe(action))
        {
            THost host = subjectBuilder.Xray.Expression.Compile().Invoke();
            Getter = () =>
                     {
                         action.Compile().Invoke(host);
                         return host;
                     };
        }

        public Func<THost> Getter { get; private set; }

        public override IVoidCollaboratingSubject<THost> Xray
        {
            get { return this; }
        }

        public override IVoidSpecification<THost> Specify(ISelfDescribingPredicate<THost> selfDescribingPredicate,
            Func<Exception, THost, IEvaluation<THost>> exceptionFilter = null)
        {
            return new VoidSpecification<THost>(this, Getter, exceptionFilter : exceptionFilter);
        }
    }

    public interface IVoidCollaboratingSubject<out THost> : ICollaboratingSubject
        where THost : class
    {
        Func<THost> Getter { get; }
    }
}
